package com.yy.show.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.yy.base.util.StringUtil;
import com.yy.module.entity.ModuleField;
import com.yy.module.entity.ModuleInfo;
import com.yy.module.entity.ModuleQueryField;

/**
 * 数据编辑模块处理类
 */
@SuppressWarnings("unchecked")
public class ModuleDeleteHandle extends ModuleHandle {
	public ModuleDeleteHandle(ModuleInfo module, HttpServletRequest request) {
		super(module, request);
	}
	
	/**
	 * 获取保存SQL
	 */
	public String getSql() {
		return sql.toString();
	}
	
	/**
	 * 判断参数格式是否正确
	 */
	public boolean hasDeleteError() {
		sql = new StringBuilder();
		
		sql.append("delete from ");
		List<ModuleField> list = module.getFieldList();
		ModuleField field = null;
		tables = new HashMap<String, String>();
		String t = null;
		
		values = new HashMap();
		Object value = null;
		String format = null;
		
		for (int i = 0, size = list.size(); i < size; i++) {
			field = list.get(i);
			t = tables.get(field.getTableName());
			//传入值是主键值
			if (field.getIsId() != null && field.getIsId() == 1) {
				continue;
			}
			//不能修改
			if (field.getIsNotUpdate() != null && field.getIsNotUpdate() == 1) {
				continue;
			}
			if (t == null) {
				t = "t" + i;
				tables.put(field.getTableName(), t);
				sql.append(field.getTableName()).append(" ").append(t);
			}
		}
		
		sql.append(" where 1=1");
		
		//处理必须查询条件
		List<ModuleQueryField> queryList = module.getQueryFieldList();
		if (queryList == null || queryList.size() == 0) {
			error = "未找到主键配置内容，请对模块进行配置！";
			return true;
		}

		ModuleQueryField query = null;
		
		for (int index = 0; index < queryList.size(); index++) {
			query = queryList.get(index);
			//如果是隐藏的，并且有默认值，处理默认值
			if (query.getIsHide() == 1 && StringUtil.isNotEmpty(query.getDefaultValue())) {
				value = getDefaultValue(query.getDefaultValue());
			} else {
				value = request.getParameter(query.getTableName() + "_" + query.getFieldName());
			}
			if (StringUtil.isEmpty(value)) {
				if (field.getIsNotNull() == 1) {
					error = "主键 [" + field.getName() + "] 不能为空！";
					return true;
				}
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" is null");
			} else {
				if (ModuleField.DATA_VALUE_DATE.equals(query.getType())) {
					if (value instanceof String) {
						if ("".equals(value)) {
							error = "主键 [" + query.getName() + "] 不能为空！";
							return true;
						} else {
							format = query.getFormat() != null && query.getFormat().length() > 7 ? query.getFormat() : "yyyy-MM-dd";
							try {
								value = new SimpleDateFormat(format).parse((String)value);
							} catch (Exception e) {
								error = "[" + query.getName() + "] 解析成时间格式 [" + format + "] 错误！";
								return true;
							}
						}
					}
				} else if (ModuleField.DATA_VALUE_NUMBER.equals(query.getType())) {
					if ("".equals(value)) {
						error = "主键 [" + query.getName() + "] 不能为空！";
						return true;
					} else {
						try {
							value = Double.parseDouble((String)value);
						} catch (Exception e) {
							error = "[" + query.getName() + "] 转为数字格式错误！";
							return true;
						}
					}
				}
				sql.append(" and ").append(tables.get(query.getTableName())).append(".").append(query.getFieldName()).append(" = :p").append(index);
				values.put("p" + index, value);
			}
		}
		return false;
	}
}